<template>
  <div id="content">
    <el-card class="box-card">
      <el-form
        :model="loginData"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            type="text"
            v-model="loginData.username"
            autocomplete="off"
            width="400px"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="loginData.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button @click="resetToken()">重置token</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import {login} from "@/api/userApi"
import request from "@/utils/request";
import { getToken, getRefreshToken } from '@/utils/auth'
export default {
    data(){
        return {
            loginData:{
                username:"",
                password:""
            },
            rules:{
                username:[
                    {request:true,message:"请输入用户名",trigger:"blur"},
                ],
                password: [
                    { required: true, message: "请输入密码", trigger: "blur" }
                ],
            }
        }
    },
    methods:{
        submitForm(formName){
            this.$refs[formName].validate((valid) => {
                if(valid) {
                    login(this.loginData)
                }else{
                    this.$message({
                        showClose: true,
                        message: "用户名或者密码不能为空，请确认重试",
                        type: "error",
                    });
                    return false;
                }
            })
        },
        resetToken(){
            let accessToken = getToken();
            let refreshToken = getRefreshToken();
            let data = {
                accessToken,
                refreshToken
            }
            console.log(data);
            if (accessToken && refreshToken) {
                return request.post("/refresh", {accessToken:getToken(),refreshToken:getRefreshToken()}).then(res=>{
                    console.log(res);
                });
            } else {
                return false;
            }
        }
            // refreshToken(); 
        },
        resetForm(formName){
            this.$refs[formName].resetFields();
        }
    }
</script>
<style scoped>
#content {
  display: flex;
  justify-content: center;
  margin-top: 180px;
}
.box-card {
  width: 600px;
}
</style>